/*
*/

#include "display.h"

#include "event.h"
#include "timer.h"

static u32	segf=0xffffffff;
static u32	segs=0xffffffff;
static u32	segt;

void Display(void){
    u8	g;
    u16	d;

    g=clock%16;
    if(g<4){
	d=(0xefff<<g)|0xfff;
	if((segf!=segs)&&((clock-segt)%1000>500))
	    GPIO_Write(GPIOB,d&(~(*((u8*)&segf+g)<<2)));
	else GPIO_Write(GPIOB,d&(~(*((u8*)&segs+g)<<2)));
    }
    else GPIO_Write(GPIOB,0xf000);
}

void DisplayDec(u32 value,u8 flash){
    u8	*p;
    u8	i;
    static
    u8 	m[]={0x40,0xf9,0x24,0x30,0x99,0x12,0x02,0x78,0x00,0x10};

    i=segs==segf;
    p=(u8*)&segs;
    *p=m[value%10];
    *p++;
    if(value/=10){
	*p=m[value%10];
	*p++;
	*p=(value/=10)?m[value%10]:255;
    }
    else{
	*p=255;
	*p++;
	*p=255;
    }
    *p++;
    *p=~(1<<page);
    segf=flash?~(1<<(page+24)):segs;
    if(i&&flash)
	segt=flash>1?clock-500:clock;
}

void DisplayStr(u32 str,u8 flash){
    u8	i;

    i=segs==segf;
    segs=~(1<<(page+24))&str;
    segf=flash?~(1<<(page+24)):segs;
    if(i&&flash)
	segt=flash>1?clock-500:clock;
}

void FlashAll(void){
    segs=0xf0ffffff;
    segf=~(1<<(page+24));
    segt=clock;
}

void FlashOff(void){
    segs=0xffffffff;
    segf=~(1<<(page+24));
    segt=clock;
}

void FlashOn(void){
    segs=~(1<<(page+24));
    segf=0xffffffff;
    segt=clock;
}

void IndicatorOff(void){
    segf=segs=0xffffffff;
}

void IndicatorOn(void){
    segs=segf=~(1<<(page+24));
}
